package com.bestcem.xm.ticket.grpc.client;

import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.deliver.grpc.v1.services.*;
import com.bestcem.xm.ticket.grpc.client.convert.TicketBaseDeliverConvert;
import com.bestcem.xm.ticket.grpc.client.dto.deliver.DeliverAgainRecordDTO;
import com.bestcem.xm.ticket.grpc.client.dto.deliver.DeliverAgainTaskDTO;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;
import zhongyan.xm.base.v1.Base;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Objects;

/**
 * 倍市得 Deliver 模块相关rpc接口客户端
 *
 * @author zq.ding <zq.ding@idiaoyan.com>
 * @version v1.0
 * @date 2021/8/27 14:05
 */
@Slf4j
@Service
public class TicketBaseDeliverGrpcService {

    @GrpcClient(GrpcClientNames.DS_DELIVER)
    BaseDeliverServiceGrpc.BaseDeliverServiceBlockingStub baseDeliverServiceBlockingStub;

    /**
     * 获取单个AgainDeliverRecord
     *
     * @param id id
     * @return 二次投放记录
     * @author <yuming.pan@idiaoyan.com>
     * @date 16:18 2021/8/30
     */
    @Nullable
    public DeliverAgainRecordDTO getAgainDeliverRecord(String id) {
        GetAgainDeliverRecordRequest.Builder builder = GetAgainDeliverRecordRequest.newBuilder();
        builder.setId(id);
        try {
            AgainDeliverRecordResponse response = baseDeliverServiceBlockingStub.getAgainDeliverRecord(builder.build());
            if (Objects.isNull(response) || Base.Code.ERROR.equals(response.getStatus().getCode())) {
                log.error("[Ticket] DELIVER getAgainDeliverRecord {} Error; ErrorMsg: {}", id, response);
                return null;
            }
            return TicketBaseDeliverConvert.responseToDeliverAgainRecordDTO(response);
        } catch (Exception e) {
            log.error("[Ticket] DELIVER getAgainDeliverRecord {} Error; ErrorMsg: {}", id, e.getMessage(), e);
            return null;
        }
    }

    /**
     * 通过查询条件批量一次性获取所有符合条件的AgainDeliverTask
     *
     * @param orgID        组织id
     * @param rawProjectId 原始问卷id
     * @return 二次投放任务
     * @author <yuming.pan@idiaoyan.com>
     * @date 16:40 2021/8/30
     */
    @Nullable
    public List<DeliverAgainTaskDTO> listAgainDeliverTask(String orgID, String rawProjectId) {
        ListAgainDeliverTaskRequest.Builder builder = ListAgainDeliverTaskRequest.newBuilder();
        AgainDeliverTask.Builder againDeliverTaskBuilder = AgainDeliverTask.newBuilder();
        againDeliverTaskBuilder.setOrgId(orgID);
        againDeliverTaskBuilder.setRawProjectId(rawProjectId);
        builder.setAgainDeliverTask(againDeliverTaskBuilder.build());
        try {
            AgainDeliverTaskSearchResponse response = baseDeliverServiceBlockingStub.listAgainDeliverTask(builder.build());
            if (Objects.isNull(response) || Base.Code.ERROR.equals(response.getStatus().getCode())) {
                log.error("[Ticket] DELIVER listAgainDeliverTask {},{} Error; ErrorMsg: {}", orgID, rawProjectId, response);
                return null;
            }
            return TicketBaseDeliverConvert.responseToDeliverAgainTaskDTOList(response);
        } catch (Exception e) {
            log.error("[Ticket] DELIVER listAgainDeliverTask {},{} Error; ErrorMsg: {}", orgID, rawProjectId, e.getMessage(), e);
            return null;
        }
    }
}
